package com.bhs.sansonglogistics.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.MenuBean;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * 底部选择弹窗
 */
public class BottomSelectListDialog extends Dialog {

    private RecyclerView mRvList;
    private List<MenuBean> list;
    private BaseQuickAdapter<MenuBean, BaseViewHolder> mAdapter;

    public BottomSelectListDialog(@NonNull Context context, List<MenuBean> list) {
        super(context, R.style.MyBottomDialogStyle);
        this.list = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bottom_select_list);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width =WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp); //将属性设置给窗体
        initView();
    }

    private void initView() {
        mRvList = findViewById(R.id.rv_list);
        mAdapter = new BaseQuickAdapter<MenuBean, BaseViewHolder>(R.layout.item_select, list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, MenuBean item) {
                helper.setText(R.id.tv_name, item.getMenuName());
                helper.setImageResource(R.id.iv_icon, item.getMenuIcon());
                helper.setGone(R.id.iv_icon, item.getMenuIcon() > 0);
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                dismiss();
                myClickListener.onClick(list.get(position).getMenuName(),null);
            }
        });

    }
    private MyClickListener2 myClickListener;
    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.myClickListener = l;
    }
}
